<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('话费充值订单列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <p>商家订单：</p>
                            <input type="text" name="orderNum"/>
                        </li>
                        <li>
                            <p>充值号码：</p>
                            <input type="text" name="mobile"/>
                        </li>
                        <li>
                            <p>充值类型：</p>
                            <select name="rechargeChannel" th:with="type=${@dict.getType('sd_recharge_channel')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <p>订单状态：</p>
                            <select name="state">
                                <option value="">所有</option>
                                <option value="1">充值中</option>
                                <option value="2">成功</option>
                                <option value="3">失败</option>
                            </select>
                        </li>
                        <li>
                            <p>结算状态：</p>
                            <select name="settleStatus" th:with="type=${@dict.getType('sd_recharge_settlestatus')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="business:order:add">
                <i class="fa fa-plus"></i> 单个充值
            </a>
            <a class="btn btn-info" onclick="$.table.importExcel()" shiro:hasPermission="business:order:import">
                <i class="fa fa-upload"></i> 批量充值
            </a>
            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="business:order:export">
                <i class="fa fa-download"></i> 导出
            </a>
            <a class="btn btn-danger" onclick="settle()" shiro:hasPermission="business:order:settle">
                <i class="fa fa-money"></i> 结算
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    let editFlag = [[${@permission.hasPermi('business:order:edit')}]];
    let removeFlag = [[${@permission.hasPermi('business:order:remove')}]];
    let adminFlag = [[${@permission.isRole('admin')}]];
    let rechargeChannelDatas = [[${@dict.getType('sd_recharge_channel')}]];
    let rechargeMoneyDatas = [[${@dict.getType('sd_recharge_money')}]];
    let rechargeSettlestatusDatas = [[${@dict.getType('sd_recharge_settlestatus')}]];
    let prefix = ctx + "business/order";

    $(function () {
        let options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            importUrl: prefix + "/importData",
            importTemplateUrl: prefix + "/importTemplate",
            modalName: "话费充值订单",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '主键id'
                },
                {
                    field: 'orderNum',
                    title: '商家订单号'
                },
                {
                    field: 'orderNumUp',
                    title: '上游订单号'
                },
                {
                    field: 'mobile',
                    title: '充值号码'
                },
                {
                    field: 'rechargeChannel',
                    title: '充值类型',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(rechargeChannelDatas, value);
                    }
                },
                {
                    field: 'state',
                    title: '订单状态',
                    formatter: function (value) {
                        switch (value) {
                            case 1:
                                return '充值中';
                            case 2:
                                return '成功';
                            case 3:
                                return '失败';
                            default:
                                return value + '未知状态';
                        }
                    }
                },
                {
                    field: 'rechargeMoney',
                    title: '充值面额',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(rechargeMoneyDatas, value);
                    }
                },
                {
                    field: 'money',
                    title: '金额'
                },
                {
                    visible: adminFlag,
                    field: 'rechargeResult',
                    title: '充值结果',
                    formatter: function (value, index, row) {
                        return $.table.tooltip(value, 20, 'open')
                    }
                },
                {
                    visible: adminFlag,
                    field: 'settleStatus',
                    title: '结算状态',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(rechargeSettlestatusDatas, value);
                    }
                }]
        };
        $.table.init(options);
    });

    function settle() {
        let rows = $($.table.getOptionsIds()).bootstrapTable('getSelections');
        if (rows.length === 0) {
            $.modal.alertWarning("至少选择一条记录进行结算");
            return;
        }

        if (!rows.every(function (value, index, self) {
            return value.state === 2 && value.settleStatus === 0;
        })) {
            $.modal.alertWarning("只能结算充值成功且未结算过的记录");
            return;
        }

        let ids = $.map(rows, function (row) {
            return row.id;
        });

        $.modal.confirm('确认要结算选中的' + ids.length + '条记录吗？', function () {
            $.operate.post(prefix + '/settle', {ids: ids.join(',')})
        })
    }
</script>
<!-- 导入区域 -->
<script id="importTpl" type="text/template">
    <form enctype="multipart/form-data" class="mt20 mb10">
        <div class="col-xs-offset-1">
            <input type="file" id="file" name="file"/>
            <div class="mt10 pt5">
                <a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> 下载模板</a>
            </div>
            <span class="pull-left mt10" style="color: red; ">
			提示：仅允许导入“xls”或“xlsx”格式文件！
		</span>
        </div>
    </form>
</script>
</body>
</html>